package com.fxf.srb.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxf.common.exception.Assert;
import com.fxf.common.result.ResponseEnum;
import com.fxf.srb.core.enums.UserBindEnum;
import com.fxf.srb.core.hfb.FormHelper;
import com.fxf.srb.core.hfb.HfbConst;
import com.fxf.srb.core.hfb.RequestHelper;
import com.fxf.srb.core.mapper.UserInfoMapper;
import com.fxf.srb.core.pojo.entity.UserBind;
import com.fxf.srb.core.mapper.UserBindMapper;
import com.fxf.srb.core.pojo.entity.UserInfo;
import com.fxf.srb.core.pojo.vo.UserBindVo;
import com.fxf.srb.core.service.IUserBindService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author fxf
 * @since 2021-05-26
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements IUserBindService {

    @Resource
    private UserInfoMapper userInfoMapper;

    /**
     * 账户绑定提交到托管平台的数据
     * @param userBindVO
     * @param userId
     * @return
     */
    @Override
    public String commitBindUser(UserBindVo userBindVO, Long userId) {

        //判断身份证号是否已绑定
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card",userBindVO.getIdCard())
                .ne("user_id",userId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        //查询用户绑定信息
        QueryWrapper<UserBind> wrapper1 = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        userBind = baseMapper.selectOne(wrapper1);

        //判断是否有绑定记录  如果未创建绑定记录，则创建一条记录
        if (userBind == null){
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVO, userBind);
            userBind.setId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        }else {
            // 曾经跳转到托管平台，但是未操作完成，此时将用户最新填写的数据同步到userBind对象
            BeanUtils.copyProperties(userBindVO,userBind);
            baseMapper.updateById(userBind);
        }
        Map<String, Object> paramMaps = new HashMap<>();

        paramMaps.put("agent_id",HfbConst.AGENT_ID);
        paramMaps.put("agent_user_id",userId);
        paramMaps.put("id_card",userBind.getIdCard());
        paramMaps.put("personal_name",userBind.getName());
        paramMaps.put("bank_type",userBind.getBankType());
        paramMaps.put("bank_no",userBind.getBankNo());
        paramMaps.put("mobile",userBind.getMobile());
//        paramMaps.put("email",userBind.getE)
        paramMaps.put("return_url",HfbConst.USERBIND_RETURN_URL);
        paramMaps.put("notify_url",HfbConst.USERBIND_NOTIFY_URL);
        paramMaps.put("timestamp", RequestHelper.getTimestamp());
        paramMaps.put("sign",RequestHelper.getSign(paramMaps));

        //构建充值自动提交表单
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMaps);
        return formStr;
    }

    /**
     * 账户绑定异步回调实现x
     * @param paramMap
     */
    @Override
    @Transactional
    public void notify(Map<String, Object> paramMap) {

        String bindCode = (String) paramMap.get("bindCode");
        //会员id
        String agentUserId = (String) paramMap.get("agentUserId");

        //根据user_id查询user_bind记录
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",agentUserId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        //更新用户绑定表
        baseMapper.updateById(userBind);

        //更新用户表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setBindCode(bindCode);
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoMapper.updateById(userInfo);

    }

    /**
     * 根据用户id查询bindCode
     * @param userId
     * @return
     */
    @Override
    public String getBindCodeByUserId(Long userId) {

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        return userInfo.getBindCode();
    }
}
